package com.wego.controller;

import com.wego.bean.TableBean;
import com.wego.common.bean.PageBean;
import com.wego.common.formatter.LocalDateTimeFormatter;
import com.wego.entity.domain.Comment;
import com.wego.entity.query.CommentQuery;
import com.wego.entity.vo.CommentVO;
import com.wego.service.CommentService;
import com.wego.service.GoodsService;
import com.wego.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * (tb_comment)表控制层
 *
 * @author Li Chuanwei
 */
@Controller
@RequestMapping("/manager/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public @ResponseBody TableBean list(CommentQuery commentQuery) {
        if(commentQuery.getStartTime() != null && commentQuery.getStartTime().equals(""))
            commentQuery.setStartTime(null);
        if(commentQuery.getEndTime() != null && commentQuery.getEndTime().equals(""))
            commentQuery.setEndTime(null);
        final PageBean<Comment> commentPageBean = commentService.selectPage(commentQuery);
        PageBean<CommentVO> pageBean = commentPageBean2CommentVOPageBean(commentPageBean);
        List<CommentVO> commentVOS = pageBean.getResult();
        Long count = pageBean.getTotal();
        TableBean tableBean = new TableBean<CommentVO>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(commentVOS);
        tableBean.setCount(count);
        return tableBean;
    }

    private PageBean<CommentVO> commentPageBean2CommentVOPageBean(PageBean<Comment> pageBean) {
        PageBean<CommentVO> commentVOPageBean = new PageBean<>();
        commentVOPageBean.setPageNum(pageBean.getPageNum());
        commentVOPageBean.setPageSize(pageBean.getPageSize());
        commentVOPageBean.setPages(pageBean.getPages());
        commentVOPageBean.setTotal(pageBean.getTotal());

        List<CommentVO> list = new ArrayList<>();
        for (Comment comment : pageBean.getResult()) {
            final CommentVO commentVO = comment2CommentVO(comment);
            list.add(commentVO);
        }
        commentVOPageBean.setResult(list);

        return commentVOPageBean;
    }

    private CommentVO comment2CommentVO(Comment comment) {
        if (comment == null) {
            return null;
        }
        CommentVO commentVO = new CommentVO();
        commentVO.setId(comment.getId());
        commentVO.setContent(comment.getContent());
        commentVO.setImgs(comment.getImgs());
        commentVO.setStars(comment.getStars());
        commentVO.setUserId(comment.getUserId());
        commentVO.setUserName(userService.selectByPrimaryKey(comment.getUserId()).getNickname());
        commentVO.setGoodsId(comment.getGoodsId());
        commentVO.setGoodsName(goodsService.selectByPrimaryKey(comment.getGoodsId()).getName());
        commentVO.setPriority(comment.getPriority());
        commentVO.setCreateTime(LocalDateTimeFormatter.formatLocalDateTime(comment.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
        return commentVO;
    }

}
